package com.smasher.camera.other

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.FrameLayout
import kotlin.math.sqrt

class PreviewContainer : FrameLayout {

    private val mGestureDetector: GestureDetector

    /**
     * 缩放相关
     */
    private var currentDistance = 0f
    private var lastDistance = 0f

    private var mOperationListener: OperationListener? = null

    fun setPreOperationListener(operationListener: OperationListener) {
        mOperationListener = operationListener
    }

    val onGestureListener: GestureDetector.OnGestureListener =
        object : GestureDetector.OnGestureListener {
            override fun onDown(e: MotionEvent): Boolean {
                Log.i("TAG", "onDown: 按下")
                return true
            }

            override fun onShowPress(e: MotionEvent) {
                Log.i("TAG", "onShowPress: 刚碰上还没松开")
            }

            override fun onSingleTapUp(e: MotionEvent): Boolean {
                Log.i("TAG", "onSingleTapUp: 轻轻一碰后马上松开")
                return true
            }

            override fun onScroll(
                e1: MotionEvent?,
                e2: MotionEvent,
                distanceX: Float,
                distanceY: Float
            ): Boolean {
                Log.i("TAG", "onScroll: 按下后拖动")
                // 大于两个触摸点
                if (e2.pointerCount >= 2) {
                    //event中封存了所有屏幕被触摸的点的信息，第一个触摸的位置可以通过event.getX(0)/getY(0)得到
                    val offSetX = e2.getX(0) - e2.getX(1)
                    val offSetY = e2.getY(0) - e2.getY(1)
                    //运用三角函数的公式，通过计算X,Y坐标的差值，计算两点间的距离
                    currentDistance =
                        sqrt((offSetX * offSetX + offSetY * offSetY).toDouble()).toFloat()
                    handleScaleGesture(currentDistance, lastDistance, mOperationListener)
                    //在一次缩放操作完成后，将本次的距离赋值给lastDistance，以便下一次判断
                    //但这种方法写在move动作中，意味着手指一直没有抬起，监控两手指之间的变化距离超过10
                    //就执行缩放操作，不是在两次点击之间的距离变化来判断缩放操作
                    //故这种将本次距离留待下一次判断的方法，不能在两次点击之间使用
                    lastDistance = currentDistance
                }
                return true
            }

            override fun onLongPress(e: MotionEvent) {
                Log.i("TAG", "onLongPress: 长按屏幕")
                mOperationListener?.longClick(e.x, e.y)
            }


            override fun onFling(
                e1: MotionEvent?,
                e2: MotionEvent,
                velocityX: Float,
                velocityY: Float
            ): Boolean {
                Log.i("TAG", "onFling: 滑动后松开")
                currentDistance = 0f
                lastDistance = 0f
                return true
            }
        }


    private fun handleScaleGesture(current: Float, last: Float, listener: OperationListener?) {
        val diff = current - last
        when {
            last == 0f -> {}
            diff > 10 -> listener?.zoomLarger()
            diff < -10 -> listener?.ZoomSmaller()
        }
    }

    private val onDoubleTapListener: GestureDetector.OnDoubleTapListener =
        object : GestureDetector.OnDoubleTapListener {
            override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
                Log.i("TAG", "onSingleTapConfirmed: 严格的单击")
                mOperationListener?.click(e.x, e.y)
                return true
            }

            override fun onDoubleTap(e: MotionEvent): Boolean {
                Log.i("TAG", "onDoubleTap: 双击")
                mOperationListener?.doubleClick(e.x, e.y)
                return true
            }

            override fun onDoubleTapEvent(e: MotionEvent): Boolean {
                Log.i("TAG", "onDoubleTapEvent: 表示发生双击行为")
                return true
            }
        }


    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    );

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)


    init {
        mGestureDetector = GestureDetector(context, onGestureListener)
        mGestureDetector.setOnDoubleTapListener(onDoubleTapListener)

        // mScaleGestureDetector = new ScaleGestureDetector(context, onScaleGestureListener);
        // 解决长按屏幕无法拖动,但是会造成无法识别长按事件
        // mGestureDetector.setIsLongpressEnabled(false);
    }


    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        // 接管onTouchEvent
        return mGestureDetector.onTouchEvent(event)
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }


}

